/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-5-14 下午1:35:50
 */
package com.absir.android.app;

import android.app.Activity;
import android.app.Instrumentation;
import android.os.Bundle;

import com.absir.android.context.ContextUtils;
import com.absir.android.view.InjectViewUtils;
import com.absir.bean.core.BeanFactoryParameters;
import com.absir.bean.core.BeanFactoryUtils;
import com.absir.bean.inject.InjectInvoker;

/**
 * @author absir
 * 
 */
public class InstrumentationBean extends Instrumentation {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.app.Instrumentation#callActivityOnCreate(android.app.Activity,
	 * android.os.Bundle)
	 */
	@Override
	public void callActivityOnCreate(Activity activity, Bundle icicle) {
		try {
			InjectInvoker[][] injectInvokers = ContextUtils.getCreateInvokers(activity.getClass());
			BeanFactoryParameters beanFactoryParameters = new BeanFactoryParameters(BeanFactoryUtils.get(), new Object[] { icicle });
			for (InjectInvoker injectInvoker : injectInvokers[0]) {
				beanFactoryParameters.resetParameterIndex();
				injectInvoker.invoke(beanFactoryParameters, activity);
			}

			super.callActivityOnCreate(activity, icicle);
			InjectViewUtils.onCreate(activity, icicle);
			for (InjectInvoker injectInvoker : injectInvokers[1]) {
				beanFactoryParameters.resetParameterIndex();
				injectInvoker.invoke(beanFactoryParameters, activity);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.app.Instrumentation#callActivityOnDestroy(android.app.Activity)
	 */
	@Override
	public void callActivityOnDestroy(Activity activity) {
		try {
			BeanFactoryUtils.onDestroy(activity);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		super.callActivityOnDestroy(activity);
	}
}
